package com.sjg.sys.controller;

import com.sjg.sys.entity.SysReturnInfo;
import com.sjg.sys.entity.SysUser;
import com.sjg.sys.service.SysUserService;
import com.sjg.sys.utils.CommUtil;
import com.sjg.sys.utils.Md5Util;
import com.sjg.sys.utils.SysConstant;
import com.sjg.sys.dao.SysRedisDao;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by fuqingjian on 2017/1/14.
 */
@Controller
@RequestMapping("/")
public class MainController {
    @Resource
    private SysRedisDao redisUtil;
    @Resource
    private SysUserService sysUserService;
    @RequestMapping("getuser")
    @ResponseBody
    public SysUser getuser(){
        SysUser user=new SysUser();
//        user.setId(123L);
//        user.setUserCode("abcde");
//        user.setLocked(1);
//        user.setLoginName("fqj1314");
//        user.setName("付青建");

        return user;
    }
    @RequestMapping("setuser")
    public void setuser(SysUser user){
        System.out.println(user.getName());
    }
    @RequestMapping("login")
    public String index(HttpServletRequest request){
        return "login";
    }

    @RequestMapping("loginin")
    @ResponseBody
    public SysReturnInfo login(HttpServletRequest request, SysUser userParams){
        String pwd= new String(Base64.decodeBase64(userParams.getLoginPwd()));
        // 传入登录名为空时返回
        if (StringUtils.isBlank(userParams.getLoginName())) {
            return CommUtil.rtnmsg(false, "用户必填","user");
        }
        // 传入密码为空时返回
        if (StringUtils.isBlank(userParams.getLoginPwd())) {
            return CommUtil.rtnmsg(false, "密码必填","pwd");
        }
        // 通过登录名查询用户
        SysUser user=sysUserService.selectUnlockedUser(userParams.getLoginName());
        if (user == null) {
            return CommUtil.rtnmsg(false, "账号不存在或被禁用", "user");
        }
        /*前面明文，后面加密文*/
        Boolean stauts= Md5Util.validate(pwd,user.getLoginPwd());
        if (!stauts) {
            return CommUtil.rtnmsg(false, "密码错误", "pwd");
        }
        request.getSession().setAttribute(SysConstant.SESSION_ATTR_USER, user);
        request.getSession().setMaxInactiveInterval(30 * 60);//单位秒
//        request.getSession().setMaxInactiveInterval(6);//单位秒
        return CommUtil.rtnmsg(true,"登陆成功！");
    }
    @RequestMapping("loginout")
    public String loginOut(HttpServletRequest request){
        request.getSession().removeAttribute(SysConstant.SESSION_ATTR_USER);
        request.getSession().invalidate();
        return "redirect:login.html";
    }

    @RequestMapping("main")
    public String main(HttpServletRequest request){
        return "main";
    }

    @RequestMapping("todaylist")
    public String todayList(HttpServletRequest request){
        return "todayList";
    }
}
